use aho_corasick::{AhoCorasick, PatternID};
#[cfg(feature = "ssr")]
pub (super) fn parse_exp(exp: &str) -> anyhow::Result<(Vec<&str>,String)> {
    let pat = AhoCorasick::new(vec!["{", "}"]).unwrap();
    let mut start:Option<usize>=None;
    let mut col_names:Vec<&str> = Vec::new();
    let mut exp2:String=String::new();
    for m in pat.find_iter(exp) {
        if m.pattern()==PatternID::must(0) {
            if m.start()>0 {
                let b=start.unwrap_or(0);
                exp2.push_str(&exp[b..m.start()]);
            }
            start = Some(m.start()+1);
        }else if m.pattern()==PatternID::must(1) {
            if let Some(s) = start {
                let col_name = &exp[s..m.start()];
                col_names.push(col_name);
                exp2.push_str(col_name);
                start = Some(m.start()+1);
            }else{
                anyhow::bail!("错误的表达式:{exp},字段名必须以大括号开头，以大括号结尾")
            }
        }
    }
    if let Some(start)=start {
        if start<exp.len() {
            exp2.push_str(&exp[start..]);
        }
    }
    Ok((col_names,exp2))
}
#[cfg(feature = "ssr")]
pub fn get_exp_col_names(exp: &str) -> anyhow::Result<Vec<&str>> {
    let (clos,_)=parse_exp(exp)?;
    Ok(clos)
}


